﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using SharpSoda.Message;

namespace SharpSoda.Remote
{
    public class HttpRemoteChannel : IRemoteChannel
    {
        private string m_RemoteUri = "";
        private WebClient m_Client = new WebClient();
        
        public HttpRemoteChannel(string uri)
        {
            m_RemoteUri = uri;
        }

        public string GetUri()
        {
            return m_RemoteUri;
        }

        public async Task<string> Request(string param)
        {
            m_Client.Headers[HttpRequestHeader.ContentType] = "text/plain";
            return await m_Client.UploadStringTaskAsync(m_RemoteUri, param);
        }

        public async Task<TReply> Request<TRequest, TReply>(TRequest param)
            where TRequest : class
            where TReply : class
        {
            string content = JsonHelper.ToString<TRequest>(param);
            m_Client.Headers[HttpRequestHeader.ContentType] = "application/json";

            string reply = await m_Client.UploadStringTaskAsync(m_RemoteUri, content);

            return JsonHelper.FromString<TReply>(reply);

        }
    }
}
